using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using CollabApp.Domain.Services.Auth;

namespace CollabApp.API.Controllers;
    public class RegisterDto
    {
        public string Username { get; set; } = string.Empty;
        public string Password { get; set; } = string.Empty;
        public string ConfirmPassword { get; set; } = string.Empty;
        public string Avatar { get; set; } = string.Empty;
    }

    public class LoginDto
    {
        public string Username { get; set; } = string.Empty;
        public string Password { get; set; } = string.Empty;
        public bool RememberMe { get; set; } = false;
    }

    public class ForgotPasswordDto
    {
        public string Username { get; set; } = string.Empty;
        public string NewPassword { get; set; } = string.Empty;
    }

    public class RefreshTokenDto
    {
        public string RefreshToken { get; set; } = string.Empty;
    }

    [ApiController]
    [Route("api/[controller]")]
    public class AuthController : ControllerBase
    {
        private readonly IAuthService _authService;
        public AuthController(IAuthService authService)
        {
            _authService = authService;
        }

        [HttpPost("register")]
        public async Task<IActionResult> Register([FromBody] RegisterDto dto)
        {
            var result = await _authService.RegisterAsync(dto.Username, dto.Password, dto.ConfirmPassword, dto.Avatar);
            return Ok(result);
        }

        [HttpPost("login")]
        public async Task<IActionResult> Login([FromBody] LoginDto dto)
        {
            var result = await _authService.LoginAsync(dto.Username, dto.Password, dto.RememberMe);
            return Ok(result);
        }

        [HttpPost("forgot-password")]
        public async Task<IActionResult> ForgotPassword([FromBody] ForgotPasswordDto dto)
        {
            var result = await _authService.ForgotPasswordAsync(dto.Username, dto.NewPassword);
            return Ok(result);
        }

        [HttpPost("refresh-token")]
        public async Task<IActionResult> RefreshToken([FromBody] RefreshTokenDto dto)
        {
            var result = await _authService.RefreshTokenAsync(dto.RefreshToken);
            return Ok(result);
        }

        [HttpGet("verify")]
        public IActionResult Verify()
        {
            try
            {
                // 获取用户身份信息
                var userIdClaim = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
                if (userIdClaim == null || !Guid.TryParse(userIdClaim.Value, out var userId))
                {
                    return Unauthorized(new { code = 4001, message = "用户身份无效" });
                }

                var usernameClaim = User.FindFirst("unique_name");
                var username = usernameClaim?.Value ?? "Unknown";

                // 返回真实的用户信息
                var response = new
                {
                    code = 1000,
                    message = "验证成功",
                    data = new
                    {
                        id = userId.ToString(),
                        username = username,
                        nickname = username, // 可以后续从数据库获取
                        avatarUrl = "",
                        status = "online",
                        createdAt = DateTime.UtcNow
                    }
                };
                return Ok(response);
            }
            catch
            {
                return StatusCode(500, new { code = 5000, message = "服务器内部错误" });
            }
        }
    }